/*
 * Created by JFormDesigner on Sat Mar 26 19:28:12 CST 2022
 */

package com.wangcanxuan.www.view.user;

import java.awt.*;
import java.awt.event.*;

import com.wangcanxuan.www.controller.Impl.LikeControllerImpl;
import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.LikeController;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.Role;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;
import com.wangcanxuan.www.util.ConnectionPoolUtil;
import com.wangcanxuan.www.view.tribe.TribalHallView;

import javax.swing.*;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 主页界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class MainView extends JFrame {
    private final UserController userController;
    private final LikeController likeController;
    ResultInfo<Role> role;
    private final User user;

    public MainView(User user) {
        this.user = user;
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        likeController = ControllerProxy.getProxyInstance(LikeControllerImpl.class);
        role = userController.getUserRole(user.getId());
        initComponents();
        setVisible(true);
        initMessage();
    }

    private void initMessage() {
        nickNameText.setText(user.getNickName());
        ageText.setText(String.valueOf(user.getAge()));
        if (user.getCamp() == 0) {
            campText.setText("光明阵营");
        } else {
            campText.setText("黑暗阵营");
        }
        coinsText.setText(String.valueOf(user.getCoins()));
        roleText.setText(role.getObj().getRoleName());
        genderText.setText(user.getGender());
        ResultInfo<Long> longResultMessage = likeController.selectLikesForUser(user.getId());
        likesText.setText(String.valueOf(longResultMessage.getObj()));
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    private void exit(ActionEvent e) {
        // TODO add your code here
        if (JOptionPane.showConfirmDialog(null
                , "退出", "提示 ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            dispose();
            ConnectionPoolUtil.getInstance().closeConnectionPool();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
            System.exit(0);
        }
    }

    private void signIn(ActionEvent e) {
        // TODO add your code here
        ResultInfo<?> resultMessage = userController.signIn(user.getId());
        int code = resultMessage.getCode();
        String message = resultMessage.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            ResultInfo<User> refresh = userController.refresh(user.getId());
            coinsText.setText(String.valueOf(refresh.getObj().getCoins()));
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
        }
    }

    private void checkTribe(ActionEvent e) {
        // TODO add your code here
        new TribalHallView(user);
        dispose();
    }

    private void checkNotice(ActionEvent e) {
        // TODO add your code here
        new CheckNoticeView(user);
        dispose();
    }

    private void updateUserMessage(ActionEvent e) {
        // TODO add your code here
        new UpdateUserMessageView(user);
        dispose();
    }

    private void refresh(ActionEvent e) {
        // TODO add your code here
        ResultInfo<User> refresh = userController.refresh(user.getId());
        int code = refresh.getCode();
        String message = refresh.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new MainView(refresh.getObj());
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
        }
    }


    private void exitLogin(ActionEvent e) {
        // TODO add your code here
        new LoginOnView();
        dispose();
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        updateUserMessageButton = new JButton();
        checkTribeButton = new JButton();
        checkNoticeButton = new JButton();
        signInButton = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        nickNameText = new JTextField();
        coinsText = new JTextField();
        ageText = new JTextField();
        roleText = new JTextField();
        campText = new JTextField();
        exitButton = new JButton();
        label6 = new JLabel();
        likesText = new JTextField();
        refreshButton = new JButton();
        label7 = new JLabel();
        genderText = new JTextField();
        exitLoginButton = new JButton();
        label8 = new JLabel();

        //======== this ========
        setTitle("Dream\u4e3b\u754c\u9762");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- updateUserMessageButton ----
        updateUserMessageButton.setText("\u4fee\u6539\u4e2a\u4eba\u4fe1\u606f");
        updateUserMessageButton.addActionListener(e -> {
            updateUserMessage(e);
        });
        contentPane.add(updateUserMessageButton);
        updateUserMessageButton.setBounds(135, 295, 120, 40);

        //---- checkTribeButton ----
        checkTribeButton.setText("\u67e5\u770b\u90e8\u843d");
        checkTribeButton.addActionListener(e -> checkTribe(e));
        contentPane.add(checkTribeButton);
        checkTribeButton.setBounds(390, 295, 100, 40);

        //---- checkNoticeButton ----
        checkNoticeButton.setText("\u67e5\u770b\u901a\u77e5");
        checkNoticeButton.addActionListener(e -> checkNotice(e));
        contentPane.add(checkNoticeButton);
        checkNoticeButton.setBounds(270, 295, 109, 40);

        //---- signInButton ----
        signInButton.setText("\u6bcf\u65e5\u62c9\u4f38");
        signInButton.setIcon(null);
        signInButton.addActionListener(e -> signIn(e));
        contentPane.add(signInButton);
        signInButton.setBounds(570, 10, 145, 50);

        //---- label1 ----
        label1.setText("\u6635 \u79f0\uff1a");
        label1.setForeground(Color.white);
        contentPane.add(label1);
        label1.setBounds(50, 17, label1.getPreferredSize().width, 27);

        //---- label2 ----
        label2.setText("\u5e74 \u9f84\uff1a");
        label2.setForeground(Color.white);
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(269, 67), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u6301\u6709\u91d1\u5e01\u6570\uff1a");
        label3.setForeground(Color.white);
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(178, 22), label3.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u8eab \u4efd\uff1a");
        label4.setForeground(Color.white);
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(368, 67), label4.getPreferredSize()));

        //---- label5 ----
        label5.setText("\u6240\u5c5e\u9635\u8425\uff1a");
        label5.setForeground(Color.white);
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(368, 22), label5.getPreferredSize()));

        //---- nickNameText ----
        nickNameText.setEditable(false);
        contentPane.add(nickNameText);
        nickNameText.setBounds(102, 17, 70, nickNameText.getPreferredSize().height);

        //---- coinsText ----
        coinsText.setEditable(false);
        contentPane.add(coinsText);
        coinsText.setBounds(273, 17, 77, coinsText.getPreferredSize().height);

        //---- ageText ----
        ageText.setEditable(false);
        contentPane.add(ageText);
        ageText.setBounds(315, 62, 35, ageText.getPreferredSize().height);

        //---- roleText ----
        roleText.setEditable(false);
        contentPane.add(roleText);
        roleText.setBounds(426, 62, 85, roleText.getPreferredSize().height);

        //---- campText ----
        campText.setEditable(false);
        contentPane.add(campText);
        campText.setBounds(446, 17, 65, campText.getPreferredSize().height);

        //---- exitButton ----
        exitButton.setText("\u9000\u51fa\u7a0b\u5e8f");
        exitButton.addActionListener(e -> exit(e));
        contentPane.add(exitButton);
        exitButton.setBounds(595, 295, 104, 40);

        //---- label6 ----
        label6.setText("\u88ab \u8d5e\uff1a");
        label6.setForeground(Color.white);
        contentPane.add(label6);
        label6.setBounds(new Rectangle(new Point(50, 67), label6.getPreferredSize()));

        //---- likesText ----
        likesText.setEditable(false);
        contentPane.add(likesText);
        likesText.setBounds(102, 62, 70, likesText.getPreferredSize().height);

        //---- refreshButton ----
        refreshButton.setText("\u5237\u65b0");
        refreshButton.addActionListener(e -> refresh(e));
        contentPane.add(refreshButton);
        refreshButton.setBounds(499, 295, 91, 40);

        //---- label7 ----
        label7.setText("\u6027\u522b\uff1a");
        label7.setForeground(Color.white);
        contentPane.add(label7);
        label7.setBounds(new Rectangle(new Point(178, 67), label7.getPreferredSize()));

        //---- genderText ----
        genderText.setEditable(false);
        contentPane.add(genderText);
        genderText.setBounds(220, 62, 40, genderText.getPreferredSize().height);

        //---- exitLoginButton ----
        exitLoginButton.setText("\u9000\u51fa\u767b\u5f55");
        exitLoginButton.addActionListener(e -> exitLogin(e));
        contentPane.add(exitLoginButton);
        exitLoginButton.setBounds(25, 300, 100, 35);

        //---- label8 ----
        label8.setIcon(new ImageIcon(MainView.class.getClassLoader().getResource("Main.png")));
        contentPane.add(label8);
        label8.setBounds(0, 0, 735, 345);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JButton updateUserMessageButton;
    private JButton checkTribeButton;
    private JButton checkNoticeButton;
    private JButton signInButton;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField nickNameText;
    private JTextField coinsText;
    private JTextField ageText;
    private JTextField roleText;
    private JTextField campText;
    private JButton exitButton;
    private JLabel label6;
    private JTextField likesText;
    private JButton refreshButton;
    private JLabel label7;
    private JTextField genderText;
    private JButton exitLoginButton;
    private JLabel label8;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
